extends Resource

class_name Card

enum CARD_TYPE { COM, ALL_ENEMY, SELF, ABILITY }

@export var card_id = -1
@export var card_type: CARD_TYPE = CARD_TYPE.COM
@export var name_lab = ""
@export var icon: Texture
@export var sound: AudioStream
@export var card_scene: PackedScene
#基础攻击
var base_damage
#攻击次数
var attack_times = 1
#防御
var shield = 0:
	set = set_shield,
	get = get_shield
#基础防御
var base_shield
#力量
var power = 0
#破甲
var armor_break = 0
#怒气
var anger = 0
#怒气消耗
var consume_anger = 0
#最高怒气消耗
var max_consume_anger = 0
#怒气加成
var anger_addition = 0
#体力
var health = 0
var desc_lab: String
var point = 0:
	set = set_point
#花费减少的点数
var reduce_point = 0
var origin_point = null
#恢复能量
var restore_point = 0
#伤害
var damage = 0:
	set = set_damage,
	get = get_damate
#抽排数
var draw_card = 0
#选择模式下最小选牌数目
var card_min = 0
#选择模式下最大选牌数目
var card_max = 0
#弃牌数
var discard_card = 0
#持续时间
var duration = 0
#最大体力
var max_health = 0
#反击
var counter_attack = 0
#本回合增长的护甲
var round_shield = 0
var type_label = ""
#虚弱
var weak = 0
#怒气加成次数
var anger_addition_times = 0
#敏捷
var agile = 0
#壁垒,回合结束不掉防御
var shield_cumulation = 0
#卡牌不回会到弃牌堆
var once = false
#本回合不能抽牌
var cant_draw_card = false

func set_point(v):
	point = v
	if origin_point == null:
		origin_point = v

func set_shield(v):
	shield = v
	if base_shield == null:
		base_shield = v

func get_shield():
	return shield + CardManager.get_global_buff_shield()

func set_damage(v):
	damage = v
	if base_damage == null:
		base_damage = v


func get_damate():
	return damage + CardManager.get_global_buff_damage()


func play_on_release(_card_view: CardView) -> void:
	pass
	
func play_on_release_after(_card_view: CardView):
	if CardManager.round_data.anger_addition_times > 0:
		CardManager.round_data.anger_addition_times -= 1
		Globals.player_state.anger_addition -= 1
		Globals.player_state.anger -= CardManager.round_data.addition_anger
		if CardManager.round_data.anger_addition_times == 0:
			CardManager.round_data.anger_addition_card.pop_back()
		
		
func select_confirm(_node):
	pass

func after_round():
	pass


func update_attribute():
	if card_type == CARD_TYPE.COM || card_type == CARD_TYPE.ALL_ENEMY:
		var temp
		if self.base_damage == null:
			temp = 0
		else:
			temp = self.base_damage
		var player_state = Globals.player_state
		if player_state == null:
			self.damage = temp
		else:
			self.damage = temp + player_state.power

func get_desc() -> String:
	return ""


func get_desc_on_target(_target) -> String:
	return ""


func init_from_csv(introduce):
	self.type_label = int(introduce[1])
	self.point = int(introduce[2])
	self.desc_lab = introduce[4]


func get_final_damage(target = null) -> int:
	if CardManager.round_data.anger_addition_times > 0:
		var c = CardManager.round_data.anger_addition_card[0] 
		@warning_ignore("integer_division")
		var times = Globals.player_state.anger / c.consume_anger
		@warning_ignore("integer_division")
		var base_times = c.max_consume_anger / c.consume_anger
		if times > base_times:
			times = base_times
		var add_damage = times * c.anger_addition
		CardManager.round_data.addition_anger = times * c.consume_anger
		return Globals.get_final_damage(Globals.player_state, target, damage, add_damage)
	else:
		return Globals.get_final_damage(Globals.player_state, target, damage, 0)

func get_final_damage_str(target = null) -> String:
	var all_damage = get_final_damage(target)
	if CardManager.round_data.anger_addition_times > 0:
		CardManager.round_data.anger_addition_times += 1
		CardManager.round_data.anger_addition_card.push_back(self)
		var no_anger_damage = Globals.get_final_damage(Globals.player_state, target, damage, 0)
		var anger_damage = all_damage - no_anger_damage
		var damage_str = ""
		if anger_damage == 0:
			damage_str = str(all_damage)
		else:
			damage_str = str(all_damage) + "(" + str(anger_damage) + ")"
		CardManager.round_data.anger_addition_times -= 1
		CardManager.round_data.anger_addition_card.pop_back()
		return damage_str
	else:
		return str(all_damage)

func get_final_shield() -> int:
	if CardManager.round_data.anger_addition_times > 0:
		var c = CardManager.round_data.anger_addition_card[0]  
		@warning_ignore("integer_division")
		var times = Globals.player_state.anger / c.consume_anger
		@warning_ignore("integer_division")
		var base_times = c.max_consume_anger / c.consume_anger
		if times > base_times:
			times = base_times
		var add_shield = times * c.anger_addition
		CardManager.round_data.addition_anger = times * c.consume_anger
		return Globals.get_final_shield(Globals.player_state, shield, add_shield)
	else:
		return Globals.get_final_shield(Globals.player_state, shield)
		
func get_final_shield_str() -> String:
	var all_shield = get_final_shield()
	if CardManager.round_data.anger_addition_times > 0:
		CardManager.round_data.anger_addition_times += 1
		CardManager.round_data.anger_addition_card.push_back(self)
		var no_anger_shield = Globals.get_final_shield(Globals.player_state, self.shield)
		var anger_shield = all_shield - no_anger_shield
		var shield_str = ""
		if anger_shield == 0:
			shield_str = str(all_shield)
		else:
			shield_str = str(all_shield) + "(" + str(anger_shield) + ")"
		CardManager.round_data.anger_addition_times -= 1
		CardManager.round_data.anger_addition_card.pop_back()
		return shield_str
	else:
		return str(all_shield)


func get_final_heal() -> int:
	if CardManager.round_data.anger_addition_times > 0:
		var c = CardManager.round_data.anger_addition_card[0]  
		@warning_ignore("integer_division")
		var times = Globals.player_state.anger / c.consume_anger
		@warning_ignore("integer_division")
		var base_times = c.max_consume_anger / c.consume_anger
		if times > base_times:
			times = base_times
		var add_health = times * (c.anger_addition - 2)
		CardManager.round_data.addition_anger = times * c.consume_anger
		return health + add_health 
	else:
		return health


func get_final_heal_str() -> String:
	var all_heal = get_final_heal()
	if CardManager.round_data.anger_addition_times > 0:
		CardManager.round_data.anger_addition_times += 1
		CardManager.round_data.anger_addition_card.push_back(self)
		var no_anger_heal = Globals.get_final_heal(Globals.player_state, self.shield)
		var anger_heal = all_heal - no_anger_heal
		var heal_str = ""
		if anger_heal == 0:
			heal_str = str(all_heal)
		else:
			heal_str = str(all_heal) + "(" + str(anger_heal) + ")"
		CardManager.round_data.anger_addition_times -= 1
		CardManager.round_data.anger_addition_card.pop_back()
		return heal_str
	else:
		return str(all_heal)


func is_attack_card() -> bool:
	if card_type == CARD_TYPE.COM || card_type == CARD_TYPE.ALL_ENEMY:
		return true
	return false
